package com.ruoyi.menu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.menu.domain.Menu;
import com.ruoyi.menu.service.IMenuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜单Controller
 * 
 * @author ruoyi
 * @date 2024-12-25
 */
@RestController
@RequestMapping("/menu/menu")
public class MenuController extends BaseController
{
    @Autowired
    private IMenuService menuService;

    /**
     * 查询菜单列表
     */
    @PreAuthorize("@ss.hasPermi('menu:menu:list')")
    @GetMapping("/list")
    public TableDataInfo list(Menu menu)
    {
        startPage();
        List<Menu> list = menuService.selectMenuList(menu);
        return getDataTable(list);
    }

    /**
     * 导出菜单列表
     */
    @PreAuthorize("@ss.hasPermi('menu:menu:export')")
    @Log(title = "菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Menu menu)
    {
        List<Menu> list = menuService.selectMenuList(menu);
        ExcelUtil<Menu> util = new ExcelUtil<Menu>(Menu.class);
        util.exportExcel(response, list, "菜单数据");
    }

    /**
     * 获取菜单详细信息
     */
    @PreAuthorize("@ss.hasPermi('menu:menu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(menuService.selectMenuById(id));
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('menu:menu:add')")
    @Log(title = "菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Menu menu)
    {
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("@ss.hasPermi('menu:menu:edit')")
    @Log(title = "菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Menu menu)
    {
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("@ss.hasPermi('menu:menu:remove')")
    @Log(title = "菜单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(menuService.deleteMenuByIds(ids));
    }
}
